package com.ruoyi.web.controller.profiles;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ProFiles;
import com.ruoyi.service.IProFilesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 项目合同文件Controller
 *
 * @author ruoyi
 * @date 2020-06-24
 */
@Controller
@RequestMapping("/ruoyi/files")
public class ProFilesController extends BaseController {
    private String prefix = "ruoyi/files";

    @Autowired
    private IProFilesService proFilesService;

    @RequiresPermissions("ruoyi:files:view")
    @GetMapping()
    public String files() {
        return prefix + "/files";
    }

    /**
     * 查询项目合同文件列表
     */
    @RequiresPermissions("ruoyi:files:list")
    @PostMapping("/list/{ids}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("ids") Long ids) {
        startPage();
        List<ProFiles> list = proFilesService.selectProFilesListidss(ids);
        System.out.println(ids);
        return getDataTable(list);
    }

    /**
     * 导出项目合同文件列表
     */
    @RequiresPermissions("ruoyi:files:export")
    @Log(title = "项目合同文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{fPro}")
    @ResponseBody
    public AjaxResult export(@PathVariable("fPro")Long fPro,ProFiles proFiles) {
        proFiles.setfPro(fPro);
        List<ProFiles> list = proFilesService.selectProFilesList(proFiles);
        ExcelUtil<ProFiles> util = new ExcelUtil<ProFiles>(ProFiles.class);
        return util.exportExcel(list, "项目合同文件");
    }

    /**
     * 新增项目合同文件
     */
    @GetMapping("/add/{ids}")
    public String add(@PathVariable("ids") Long ids, ModelMap map) {
        map.put("ids", ids);
        return prefix + "/add";
    }

    /**
     * 新增保存项目合同文件
     */
    @RequiresPermissions("ruoyi:files:add")
    @Log(title = "项目合同文件", businessType = BusinessType.INSERT)
    @PostMapping("/add/{idss}")
    @ResponseBody
    public AjaxResult addSave(@PathVariable("idss") Long idss, ProFiles proFiles, @RequestParam("file") MultipartFile file) throws IOException {
        // 上传文件路径
        String filePath = Global.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        proFiles.setfUpload(fileName);

        proFiles.setfPro(idss);
        proFiles.setfTime(new Date());
        return toAjax(proFilesService.insertProFiles(proFiles));
    }

    /**
     * 修改项目合同文件
     */
    @GetMapping("/edit/{fId}")
    public String edit(@PathVariable("fId") Long fId, ModelMap mmap) {
        ProFiles proFiles = proFilesService.selectProFilesById(fId);
        mmap.put("proFiles", proFiles);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目合同文件
     */
    @RequiresPermissions("ruoyi:files:edit")
    @Log(title = "项目合同文件", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProFiles proFiles) {
        return toAjax(proFilesService.updateProFiles(proFiles));
    }

    /**
     * 删除项目合同文件
     */
    @RequiresPermissions("ruoyi:files:remove")
    @Log(title = "项目合同文件", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(proFilesService.deleteProFilesByIds(ids));
    }

    /**
     * 向项目合同文件传合同的id
     */
    @GetMapping("/files/{bId}")
    public String filesId(@PathVariable("bId") Long bId, ModelMap map) {
        map.put("bId", bId);
        return prefix + "/files";
    }

    /**
     * 本地资源通用下载
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("fUpload") String fAccessory, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(fAccessory, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }

}
